package com.ruoyi.system.service.impl;

import java.util.List;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.util.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IAUserRecordService;

/**
 * 用户查看记录Service业务层处理
 *
 * @author gf
 * @date 2025-01-01
 */
@Service
public class AUserRecordServiceImpl implements IAUserRecordService {
    @Autowired
    private AUserRecordMapper aUserRecordMapper;

    @Autowired
    private ConfigureGlobalMapper configureGlobalMapper;

    @Autowired
    private AUserMapper aUserMapper;

    @Autowired
    private AOrderMapper aOrderMapper;

    @Autowired
    private AVipUserMapper aVipUserMapper;

    @Autowired
    private AVipUserDetailMapper aVipUserDetailMapper;

    /**
     * 查询用户查看记录
     *
     * @param id 用户查看记录主键
     * @return 用户查看记录
     */
    @Override
    public AUserRecord selectAUserRecordById(Long id) {
        return aUserRecordMapper.selectAUserRecordById(id);
    }

    /**
     * 查询用户查看记录列表
     *
     * @param aUserRecord 用户查看记录
     * @return 用户查看记录
     */
    @Override
    public List<AUserRecord> selectAUserRecordList(AUserRecord aUserRecord) {
        return aUserRecordMapper.selectAUserRecordList(aUserRecord);
    }

    /**
     * 新增用户查看记录
     *
     * @param aUserRecord 用户查看记录
     * @return 结果
     */
    @Override
    public int insertAUserRecord(AUserRecord aUserRecord) {
        aUserRecord.setCreateTime(DateUtils.getNowDate());
        return aUserRecordMapper.insertAUserRecord(aUserRecord);
    }

    /**
     * 修改用户查看记录
     *
     * @param aUserRecord 用户查看记录
     * @return 结果
     */
    @Override
    public int updateAUserRecord(AUserRecord aUserRecord) {
        aUserRecord.setUpdateTime(DateUtils.getNowDate());
    return aUserRecordMapper.updateAUserRecord(aUserRecord);
    }

    /**
     * 批量删除用户查看记录
     *
     * @param ids 需要删除的用户查看记录主键
     * @return 结果
     */
    @Override
    public int deleteAUserRecordByIds(Long[] ids) {
        return aUserRecordMapper.deleteAUserRecordByIds(ids);
    }

    /**
     * 删除用户查看记录信息
     *
     * @param id 用户查看记录主键
     * @return 结果
     */
    @Override
    public int deleteAUserRecordById(Long id) {
        return aUserRecordMapper.deleteAUserRecordById(id);
    }

    /**
     * 批量逻辑删除用户查看记录
     *
     * @param ids 需要删除的用户查看记录主键
     * @return 结果
     */
    @Override
    public int logicDeleteAUserRecordByIds(Long[] ids) {
        return aUserRecordMapper.logicDeleteAUserRecordByIds(ids);
    }
    /**
     * 逻辑删除用户查看记录信息
     *
     * @param id 用户查看记录主键
     * @return 结果
     */
    @Override
    public int logicDeleteAUserRecordById(Long id) {
        return aUserRecordMapper.logicDeleteAUserRecordById(id);
    }

    @Override
    public int selectAUserRecordCheckNum(AUserRecord record) {
        int i = 1;
        Long userId = LoginUtils.getUser().getId();
//        AUser aUser = aUserMapper.selectAUserById(userId);
        record.setUserId(userId);
        //是否是高级会员
        List<AOrder> gjVip = aOrderMapper.selectAOrderByRegion(userId, record.getRegion(),"7");
        if(gjVip.size() == 0){
            //是否是普通会员
            List<AOrder> ptVip = aOrderMapper.selectAOrderByRegion(userId, record.getRegion(),"6");
            if(ptVip.size() == 0){
                //不是会员
                i = getRecord(record, i, userId);
            }else{
                ConfigureGlobal gl = configureGlobalMapper.selectConfigureGlobalById(1L);
                AVipUser aVipUser = aVipUserMapper.selectAVipUserById(1L);
                AVipUserDetail de = new AVipUserDetail();
                de.setVipId(aVipUser.getId());
                de.setHousType("1");
                List<AVipUserDetail> detailList = aVipUserDetailMapper.selectAVipUserDetailList(de);
                //查询全部次数
                List<AUserRecord> re = aUserRecordMapper.selectAUserRecordByHousId(userId);
                int num = 0;
                if(re.size() < Integer.valueOf(gl.getVipNum())){
                    num = Integer.valueOf(gl.getVipNum()) - re.size()+Integer.valueOf(detailList.get(0).getVipNum());
                }else{
                    num = Integer.valueOf(detailList.get(0).getVipNum());
                }
                //获取当前用户次数
                List<AUserRecord> res = aUserRecordMapper.selectAUserRecordByHousIdRegion(record);
                if(res.size() < num){
                    record.setCreateTime(DateUtils.getNowDate());
                    aUserRecordMapper.insertAUserRecord(record);
                    i = 0;
                }
            }
        }else{
            record.setCreateTime(DateUtils.getNowDate());
            aUserRecordMapper.insertAUserRecord(record);
            i = 0;
        }

//        //是会员随便增加记录 否则不是会员只能增加指定次数
//        if(aUser.getIsVip().equals("2")){
//            List<AOrder> aOrders = aOrderMapper.selectAOrderByRegion(userId, record.getRegion(),"7");
//            if(aOrders.size() == 0){
//                i = getRecord(record, i, userId);
//            }else{
//                record.setCreateTime(DateUtils.getNowDate());
//                aUserRecordMapper.insertAUserRecord(record);
//                i = 0;
//            }
//        }else if(aUser.getIsVip().equals("0")){
//            List<AOrder> aOrders = aOrderMapper.selectAOrderByRegion(userId, record.getRegion(),"6");
//            if(aOrders.size() == 0){
//                i = getRecord(record, i, userId);
//            }else{
//                ConfigureGlobal gl = configureGlobalMapper.selectConfigureGlobalById(1L);
//                List<AUserRecord> res = aUserRecordMapper.selectAUserRecordByHousIdRegion(record);
//                AVipUser aVipUser = aVipUserMapper.selectAVipUserById(1L);
//                AVipUserDetail de = new AVipUserDetail();
//                de.setVipId(aVipUser.getId());
//                de.setHousType("1");
//                List<AVipUserDetail> detailList = aVipUserDetailMapper.selectAVipUserDetailList(de);
//                if(res.size() < (Integer.valueOf(gl.getVipNum())+Integer.valueOf(detailList.get(0).getVipNum()))){
//                    record.setCreateTime(DateUtils.getNowDate());
//                    aUserRecordMapper.insertAUserRecord(record);
//                    i = 0;
//                }
//            }
//        }else{
//            i = getRecord(record, i, userId);
//        }
        return i;
    }

    private int getRecord(AUserRecord record, int i, Long userId) {
        ConfigureGlobal gl = configureGlobalMapper.selectConfigureGlobalById(1L);
        List<AUserRecord> re = aUserRecordMapper.selectAUserRecordByHousId(userId);
        if(re.size() < Integer.valueOf(gl.getVipNum())){
            record.setCreateTime(DateUtils.getNowDate());
            aUserRecordMapper.insertAUserRecord(record);
            i = 0;
        }
        return i;
    }

}
